﻿using PlayerScannerCore.ViewModel;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace PlayerScanner
{
    public class EquippableByConverter : IValueConverter
    {
        //There are 3 dimensions to equippability: Gender, Race, Class.

        //Bits [0 - 2] represent classes
        //Bits [3 - 5] represent races
        //Bits [6 - 7] represent gender
        //bits: 7     6     5     4     3     2     1     0
        //      F     M     Nu    Cast  Hu    FO    RA    HU     

        private const int HUMAR = 0x49;         //Male, Human, Hunter
        private const int HUNEWEARL = 0xA1;     //Female, Newman, Hunter
        private const int HUCAST = 0x51;        //Male, Cast, Hunter
        private const int HUCASEAL = 0x91;      //Female, Cast, Hunter
        private const int RAMAR = 0x4A;         //Male, Human, Ranger
        private const int RAMARL = 0x8A;        //Female, Human, Ranger
        private const int RACAST = 0x52;        //Male, Cast, Ranger
        private const int RACASEAL = 0x92;      //Female, Cast, Ranger
        private const int FOMAR = 0x4C;         //Male, Human, Force
        private const int FOMARL = 0x8C;        //Female, Human, Force
        private const int FONEWM = 0x64;        //Male, Newman, Force
        private const int FONEWEARL = 0xA4;     //Female, Newman, Force

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ret = String.Empty;

            if (value != null)
            {
                var intValue = (int)value;

                switch (intValue)
                {
                    //The following cases have been moved to the default case
                    //case 0x5B: ret = "Male human|cast hunter|ranger."; break; // excludes female newman force
                    //case 0x5F: ret = "Male human|cast hunter|ranger|force."; break; // excludes female newman
                    //case 0x79: ret = "Male hunter."; break; // excludes female ranger force
                    //case 0x7A: ret = "Male ranger."; break; // excludes female hunter force
                    //case 0x7B: ret = "Male hunter and ranger."; break; // excludes female force
                    //case 0xAC: ret = "Female force."; break; // excludes male hunter ranger cast
                    //case 0xAF: ret = "Female human and cast."; break; // excludes male newman
                    //case 0xB9: ret = "Female hunter."; break; // excludes male ranger force
                    //case 0xBA: ret = "Female ranger."; break; // excludes male hunter force
                    //case 0xBB: ret = "Female hunter and ranger."; break; // excludes male force
                    //case 0xBE: ret = "Female ranger and force."; break; // excludes male hunter
                    //case 0xCA: ret = "Human ranger."; break; // excludes hunter force cast newman
                    //case 0xD1: ret = "Cast hunter."; break; // excludes ranger force human newman
                    //case 0xD2: ret = "Cast ranger."; break; // excludes hunter force human newman
                    //case 0xD9: ret = "Human and cast hunter."; break; // excludes ranger force and newman
                    //case 0xEB: ret = "Human and Newman, Hunter and Ranger."; break; // exclude forces and cast
                    //case 0xED: ret = "HUmar, HUnewearl and Forces."; break; // excludes ranger and cast
                    //case 0xEE: ret = "RAmar, RAmarl and Forces."; break; // Excludes hunter and cast
                    case 0x00: ret = "None"; break;                 // Because there are some that actually are unequippable :v
                    case 0x49: ret = "HUmar"; break;                // excludes female ranger force cast newman
                    case 0x64: ret = "Fonewm"; break;               // excludes female hunter ranger human cast
                    case 0x7F: ret = "Males"; break;                // excludes females
                    case 0x8C: ret = "FOmarl"; break;               // excludes male hunter ranger cast newman 
                    case 0xA1: ret = "HUnewearl"; break;            // excludes male ranger force human cast
                    case 0xBF: ret = "Females"; break;              // excludes male
                    case 0xD3:                                      // excludes force human newman
                    case 0xD7: ret = "Cast only"; break;            // excludes human newman
                    case 0xFA:                                      // excludes hunter and force
                    case 0xDA: ret = "Rangers"; break;              // excludes hunter force and newman
                    case 0xFC:                                      // excludes hunter and ranger
                    case 0xEC: ret = "Forces"; break;               // excludes hunter ranger and cast
                    case 0xF9: ret = "Hunters"; break;              // excludes ranger and force
                    case 0xEF: ret = "No Cast"; break;              //"HUmar, HUnewearl, RAmar, RAmarl and Forces."; break;
                    case 0xFB:                                      // exclude force
                    case 0xDB: ret = "Hunters and Rangers"; break;  // excludes force and newman
                    case 0xFD: ret = "Hunters and Forces"; break;   // excludes ranger
                    case 0xFE: ret = "Rangers and Forces"; break;   // excludes hunter
                    case 0xFF: ret = "All Classes"; break;
                    default:
                        StringBuilder sb = new StringBuilder();
                        if ((intValue & HUMAR) == HUMAR)
                            sb.Append("HUmar ");
                        if ((intValue & HUNEWEARL) == HUNEWEARL)
                            sb.Append("HUnewearl ");
                        if ((intValue & HUCAST) == HUCAST)
                            sb.Append("HUcast ");
                        if ((intValue & HUCASEAL) == HUCASEAL)
                            sb.AppendLine("HUcaseal");
                        else
                            sb.AppendLine();
                        if ((intValue & RAMAR) == RAMAR)
                            sb.Append("RAmar ");
                        if ((intValue & RAMARL) == RAMARL)
                            sb.Append("RAmarl ");
                        if ((intValue & RACAST) == RACAST)
                            sb.Append("RAcast ");
                        if ((intValue & RACASEAL) == RACASEAL)
                            sb.AppendLine("RAcaseal");
                        else
                            sb.AppendLine();
                        if ((intValue & FOMAR) == FOMAR)
                            sb.Append("FOmar ");
                        if ((intValue & FOMARL) == FOMARL)
                            sb.Append("FOmarl ");
                        if ((intValue & FONEWM) == FONEWM)
                            sb.Append("FOnewm ");
                        if ((intValue & FONEWEARL) == FONEWEARL)
                            sb.Append("FOnewearl");
                        ret = sb.ToString().Trim();
                        break;
                }
            }

            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Do nothing
            return null;
        }
    }
}
